package com.tinusgraglin.enclusiv.datalayer.repos

import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import com.tinusgraglin.enclusiv.R

class GlobalPrefDataRepo private constructor (appContext: Context) {
    private val pref: SharedPreferences =
        appContext.getSharedPreferences(appContext.getString(R.string.main_pref), Context.MODE_PRIVATE)

    companion object {
        private var repo: GlobalPrefDataRepo? = null

        fun initialize (appContext: Context) {
            repo = GlobalPrefDataRepo(appContext)
        }

        fun get() : GlobalPrefDataRepo {
            if (repo != null)
                return repo as GlobalPrefDataRepo
            else
                throw IllegalStateException("Uninitialized")
        }
    }

    fun getString(prefName: String, default: String?) =
        pref.getString(prefName, default)

    fun getBoolean(prefName: String, default: Boolean) =
         pref.getBoolean(prefName, default)

    fun getInt(prefName: String, default: Int) =
        pref.getInt(prefName, default)

    fun edit(edit: SharedPreferences.Editor.() -> Unit) {
        pref.edit(true, edit)
    }
}